package com.wandian.notepad.mvvm.drawernotesfragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.AppUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.notepad.R;
import com.wandian.notepad.adapter.NotepadClassifyRecycleAdapter;
import com.wandian.notepad.bean.NoteTypeBean;
import com.wandian.notepad.databinding.NotepadFragmentDrawerNotesBinding;

/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description 记事本-子页面
 **/
public class DrawNotesFragment extends BaseMVVMFragment<DrawNotesFragmentViewModel>  {

    private NotepadFragmentDrawerNotesBinding dataBinding;
    private NotepadClassifyRecycleAdapter notepadClassifyRecycleAdapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = NotepadFragmentDrawerNotesBinding.inflate(inflater);
        dataBinding.setLifecycleOwner(this);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        initRecycleView();
        initViewData();
        initClink();
    }

    @Override
    public void onResume() {
        super.onResume();
        mViewModel.notepadTypeList();
    }

    private void initClink(){
        dataBinding.rlAdd.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NOTEPAD_CLASSIFY).navigation());
    }

    private void initRecycleView(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.rvClassify.setLayoutManager(layoutManager);
        notepadClassifyRecycleAdapter  = new NotepadClassifyRecycleAdapter(R.layout.notepad_item_notes_classify);
        dataBinding.rvClassify.setAdapter(notepadClassifyRecycleAdapter);

        notepadClassifyRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                NoteTypeBean noteTypeBean = (NoteTypeBean)baseQuickAdapter.getData().get(i);
                if(!UIUtils.isEmpty(noteTypeBean)){
                    Bundle bundle = new Bundle();
                    bundle.putString("type_id",noteTypeBean.getId()+"");
                    bundle.putString("type_title",noteTypeBean.getType()+"");
                    ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NOTEPAD_CLASSIFY_LIST).with(bundle).navigation();
                }
            }
        });
    }


    private void initViewData(){
        mViewModel.noteList.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean)){
                notepadClassifyRecycleAdapter.setNewData(listBean);
                notepadClassifyRecycleAdapter.notifyDataSetChanged();
            }
        });

    }


}
